package com.powernode.teatraceability.controller.circulationprocessmgt;


import com.powernode.teatraceability.pojo.circulationprocessmgt.TransportInfo;
import com.powernode.teatraceability.pojo.circulationprocessmgt.WlDetailInfo;
import com.powernode.teatraceability.pojo.login.User;
import com.powernode.teatraceability.service.basicinfomgt.EnterpriseService;
import com.powernode.teatraceability.service.circulationprocessmgt.TransportInfoService;
import com.powernode.teatraceability.utils.GlobalConfig;
import com.powernode.teatraceability.utils.JsonData;
import org.fisco.bcos.sdk.abi.datatypes.Int;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@RestController
@RequestMapping("/transport/info")
public class TransportInfoController {

    @Resource
    private TransportInfoService transportInfoService;
    @Resource
    private EnterpriseService enterpriseService;

    @GetMapping
    public JsonData countInfo(@SessionAttribute User user) {
        return JsonData.success(transportInfoService.countInfo(user.getQydm()));
    }

    @GetMapping("/{shiperName}/{pageNum}/{pageSize}")
    public JsonData selectLike(@PathVariable int pageNum,
                              @PathVariable int pageSize,
                              @PathVariable String shiperName,
                              @SessionAttribute User user){
        GlobalConfig.initPageRequest(pageNum, pageSize);
        transportInfoService.selectLike(user.getQydm(), shiperName);
        return GlobalConfig.LOCAL.get();
    }

    @GetMapping("/{pageNum}/{pageSize}")
    public JsonData selectAll(@PathVariable int pageNum,
                              @PathVariable int pageSize,
                              @SessionAttribute User user){
        GlobalConfig.initPageRequest(pageNum, pageSize);
        transportInfoService.selectAll(user.getQydm());
        return GlobalConfig.LOCAL.get();
    }

    @GetMapping("/{id}")
    public JsonData selectInfo(@PathVariable("id")Integer id) {
        final WlDetailInfo transportInfo = transportInfoService.selctById(id);
        transportInfo.setQymc(enterpriseService.selectQYMCByQydm(transportInfo.getQydm()));
        return JsonData.success(transportInfo);
    }

    @PostMapping
    public JsonData insertInfo(@RequestBody WlDetailInfo transportInfo,
                           @SessionAttribute User user){
        transportInfo.setQydm(user.getQydm());
        transportInfo.setOrderNumber(OrderNumberGenerator());
        transportInfo.setCourierNumber(ExpressNumberGenerator());
        transportInfoService.Myinsert(transportInfo);
        return JsonData.success("添加成功~");
    }

    @PutMapping
    public JsonData update(@RequestBody TransportInfo transportInfo){
        transportInfoService.update(transportInfo);
        return GlobalConfig.LOCAL.get();
    }

    public static String  OrderNumberGenerator () {
        // 获取当前年月日
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String currentDate = dateFormat.format(new Date());

        // 生成随机数
        Random random = new Random();
        long randomNum1 = random.nextLong();
        long randomNum2 = random.nextLong();

        // 将随机数转换为正数
        randomNum1 = Math.abs(randomNum1);
        randomNum2 = Math.abs(randomNum2);

        // 将随机数补足7位
        String randomStr1 = String.format("%07d", randomNum1 % 10000000);
        String randomStr2 = String.format("%07d", randomNum2 % 10000000);

        // 拼接字符串
       return currentDate + randomStr1 + randomStr2;
    }
    public static String ExpressNumberGenerator () {
        // 获取当前年月日
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String currentDate = dateFormat.format(new Date());

        // 生成随机数
        Random random = new Random();
        long randomNum1 = random.nextLong();
        long randomNum2 = random.nextLong();

        // 将随机数转换为正数
        randomNum1 = Math.abs(randomNum1);
        randomNum2 = Math.abs(randomNum2);

        // 将随机数补足8位
        String randomStr1 = String.format("%05d", randomNum1 % 10000000);
        String randomStr2 = String.format("%07d", randomNum2 % 10000000);

        // 拼接快递单号
        String prefix = "SF";
        String expressNum = prefix + currentDate + randomStr1 + randomStr2;

        // 输出快递单号
        return expressNum;
    }
}
